package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色
 *
 * @author Chengfei
 * @date 2020/08/18
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 分页查询  可带条件
     *
     * @param queryPageBean 封装 当前页码 每页记录数  查询条件
     * @return PageResult  封装  总条数  当前页面结果
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        return roleService.findPage(queryPageBean);
    }

    /**
     * 添加角色
     *
     * @param role          角色信息
     * @param permissionIds 角色拥有权限的ID集合
     * @param menuIds       角色拥有的菜单ID集合
     * @return true or false
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {

        try {
            roleService.add(role, permissionIds, menuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }

    }

    /**
     * 根据id查询角色
     *
     * @param id 角色id
     * @return 角色信息
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {

        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }

    }

    /**
     * 根据角色id查询相应权限id
     *
     * @param id 角色id
     * @return 权限id集合
     */
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {

        try {
            List<Integer> list = roleService.findPermissionIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_PERMISSIONIDS_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSIONIDS_FAIL);
        }

    }

    /**
     * 根据角色id查询菜单id
     *
     * @param id 角色id
     * @return 菜单id集合
     */
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {

        try {
            List<Integer> list = roleService.findMenuIdsByRoleId(id);
            return new Result(true, MessageConstant.GET_MENUID_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENUID_FAIL);
        }

    }

    /**
     * 编辑角色
     *
     * @param role          角色信息
     * @param menuIds       菜单id
     * @param permissionIds 权限id
     * @return true or false
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] menuIds, Integer[] permissionIds) {

        try {
            roleService.edit(role, menuIds, permissionIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }

    }

    /**
     * 根据id删除角色
     *
     * @param id 角色id
     * @return true or false
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {

        try {
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }

    }

}
